<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt />
      </div>
      <div class="login_form">
        <!-- 登录表单组件 -->
        <el-form :model="loginInfo" :rules="loginRules" ref="loginInfoRef" label-width="60px">
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginInfo.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="loginInfo.password"></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button el-button type="primary" @click="logIn">登录</el-button>
            <el-button @click="resetLogIn">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
    <script>
import { log } from "util";
export default {
  data() {
    return {
      // 登录信息
      loginInfo: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    logIn: function () {
      this.$refs.loginInfoRef.validate(async (valid) => {
        if (!valid) return;
        const res = await this.$http.post("login", this.loginInfo);
        console.log(res);
        if (res.data.meta.status !== 200) {
          return this.$message.error("登录失败");
        }
        const {data} = res.data;
        this.$message.success("登录成功");
        sessionStorage.setItem("token", res.data.data.token);
        this.$router.push({path:'/home',query:data})
      });
    },
    resetLogIn: function () {
      this.$refs.loginInfoRef.resetFields();
      this.loginInfo.name = this.loginInfo.passWord = "";
    },
  },
};
</script>
    <style lang="less" scope>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .login_box {
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 8px;
    .avatar_box {
      width: 130px;
      height: 130px;
      margin: -75px auto;
      background-color: #fff;
      border-radius: 50%;
      border: 1px solid #ccc;
      padding: 10px;
      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login_form {
      width: 300px;
      margin: 100px auto;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>